package com.zdkj.common.core.beantools;/**
 * @author 陈博
 * @date 2020/1/17 0017 19:25
 */

/**
 * @program: zdkj
 *
 * @description:
 *
 * @author: chen
 *
 * @create: 2020-01-17 19:25
 **/

import cn.hutool.core.annotation.AnnotationUtil;
import io.swagger.annotations.ApiModelProperty;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;


public class CompareFileds {
    /**
     * 获取bean name数组
     * @param o
     * @return bean name 字符数组
     */
    public static String[] getFileds(Object o){
        Set<String> set = new HashSet<>();
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            ExcludeCompareFiled anno = declaredField.getAnnotation(ExcludeCompareFiled.class);
            if (anno != null && anno.isExclude()) {
                continue;
            }
            set.add(declaredField.getName());
        }
        return set.toArray(new String[set.size()]);
    }

    /**
     * 比较两个实体属性值，返回一个map以有差异的属性名为key，value为一个list分别存obj1,obj2此属性名的值
     * @param obj1 进行属性比较的对象1
     * @param obj2 进行属性比较的对象2
     * @param compareArr 选择要比较的属性数组
     * @return 属性差异比较结果map
     */
    public static Map<String, Map> compareFields(Object obj1, Object obj2, String[] compareArr) {
        try {
            //装返回值得
            Map<String, Map> map = new LinkedHashMap<>();
            //需要对比的字段list
            List<String> ignoreList = null;
            if (compareArr != null && compareArr.length > 0) {
                // array转化为list
                ignoreList = Arrays.asList(compareArr);
            }
            // 只有两个对象都是同一类型的才有可比性
            if (obj1.getClass() == obj2.getClass()) {
                Class clazz = obj1.getClass();
                // 获取object的属性描述
                PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
                        Object.class).getPropertyDescriptors();
                // 这里就是所有的属性了
                for (PropertyDescriptor pd : pds) {
                    // 属性名
                    String name = pd.getName();
                    String annotationValue = AnnotationUtil.getAnnotationValue(Object.class, ApiModelProperty.class).toString();
                    // 如果当前属性选择进行比较，跳到下一次循环
                    if (ignoreList != null && ignoreList.contains(name)) {
                        // get方法
                        Method readMethod = pd.getReadMethod();
                        // 在obj1上调用get方法等同于获得obj1的属性值
                        Object objBefore = readMethod.invoke(obj1);
                        // 在obj2上调用get方法等同于获得obj2的属性值
                        Object objAfter = readMethod.invoke(obj2);
                        if (objBefore instanceof Timestamp) {
                            objBefore = new Date(((Timestamp) objBefore).getTime());
                        }
                        if (objAfter instanceof Timestamp) {
                            objAfter = new Date(((Timestamp) objAfter).getTime());
                        }
                        if (objBefore == null && objAfter == null) {
                            continue;
                        } else if (objBefore == null && objAfter != null) {
                            Map m = new LinkedHashMap();
                            m.put("objBefore",annotationValue+objBefore);
                            m.put("objAfter",annotationValue+objAfter);
                            map.put(name, m);
                            continue;
                        }
                        // 比较这两个值是否相等,不等则放入map
                        if (!objBefore.equals(objAfter)) {
                            Map m = new LinkedHashMap();
                            m.put("objBefore",annotationValue+objBefore);
                            m.put("objAfter",annotationValue+objAfter);
                            map.put(name, m);
                        }
                    }
                }
            }else {
                System.out.println("对象类型不一致，不能完成对比");
            }
            return map;
        } catch (Exception e) {
            System.out.println("错误");
            return null;
        }
    }
}